package com.ruoyi.project.modules.virtualUsageGroup.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.domainInfo.domain.DomainInfo;
import com.ruoyi.project.modules.domainInfo.domain.DomainObjectInfo;
import com.ruoyi.project.modules.domainInfo.service.IDomainInfoService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.request.BffParamReq;
import com.ruoyi.project.modules.interfaceinfo.request.ListInterfaceFieldParamReq;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.project.modules.virtualUsageGroupDetail.domain.VirtualUsageGroupDetail;
import com.ruoyi.project.modules.virtualUsageGroupDetail.service.IVirtualUsageGroupDetailService;
import com.ruoyi.project.modules.virtualUsageParamSource.controller.VirtualUsageParamSourceController;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.po.VirtualUsageParamSourcePo;
import com.ruoyi.project.modules.virtualUsageParamSource.service.IVirtualUsageParamSourceService;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.InterfaceObjectParam;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.ObjectParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.virtualUsageGroup.domain.VirtualUsageGroup;
import com.ruoyi.project.modules.virtualUsageGroup.service.IVirtualUsageGroupService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 虚拟用况组Controller
 *
 * @author smallrain
 * @date 2022-04-19
 */
@Controller
@RequestMapping("/virtualUsageGroup/virtualUsageGroup")
public class VirtualUsageGroupController extends BaseController {
    private String prefix = "virtualUsageGroup/virtualUsageGroup";

    @Autowired
    private IVirtualUsageGroupService virtualUsageGroupService;

    @RequiresPermissions("virtualUsageGroup:virtualUsageGroup:view")
    @GetMapping()
    public String virtualUsageGroup() {
        return prefix + "/virtualUsageGroup";
    }

    /**
     * 查询虚拟用况组列表
     */
    //@RequiresPermissions("virtualUsageGroup:virtualUsageGroup:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(VirtualUsageGroup virtualUsageGroup) {
        List<VirtualUsageGroup> list = virtualUsageGroupService.selectVirtualUsageGroupList(virtualUsageGroup);
        return AjaxResult.success(list);
    }

    /**
     * 导出虚拟用况组列表
     */
    @RequiresPermissions("virtualUsageGroup:virtualUsageGroup:export")
    @Log(title = "虚拟用况组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualUsageGroup virtualUsageGroup) {
        List<VirtualUsageGroup> list = virtualUsageGroupService.selectVirtualUsageGroupList(virtualUsageGroup);
        ExcelUtil<VirtualUsageGroup> util = new ExcelUtil<VirtualUsageGroup>(VirtualUsageGroup.class);
        return util.exportExcel(list, "虚拟用况组数据");
    }

    /**
     * 新增虚拟用况组
     */
    @GetMapping("/add")
    public String add(Long interfaceId, ModelMap mmap) {
        mmap.put("interfaceId", interfaceId);
        DomainInfo domainInfo = new DomainInfo();
        List<DomainInfo> domainInfos = this.domainInfoService.selectDomainInfoList(domainInfo);
        mmap.put("domainInfoList", domainInfos);
        List<DomainObjectInfo> domainObjectInfos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(domainInfos)) {
            DomainInfo domainInfo1 = this.domainInfoService.selectDomainInfoById(domainInfos.get(0).getId());
            if (domainInfo1 != null && domainInfo1.getDomainObjectInfoList() != null) {
                domainObjectInfos = domainInfo1.getDomainObjectInfoList();
            }
        }
        mmap.put("domainObjectList", domainObjectInfos);
        return prefix + "/addVirtualUsageGroup";
    }

    /**
     * 新增保存虚拟用况组
     */
    //@RequiresPermissions("virtualUsageGroup:virtualUsageGroup:add")
    @Log(title = "虚拟用况组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualUsageGroup virtualUsageGroup) {
        return toAjax(virtualUsageGroupService.insertVirtualUsageGroup(virtualUsageGroup));
    }
    @Resource
    private IDomainInfoService domainInfoService;
    /**
     * 修改虚拟用况组
     */
    @GetMapping("/edit")
    public String edit(Long id, Long processBranchId, ModelMap mmap) {
        VirtualUsageGroup virtualUsageGroup = virtualUsageGroupService.selectVirtualUsageGroupById(id);
        mmap.put("virtualUsageGroup", virtualUsageGroup);
        mmap.put("processBranchId", processBranchId);
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageGroup.getInterfaceId());
        mmap.put("interfaceInfo", interfaceInfo);


        DomainInfo domainInfo = new DomainInfo();
        List<DomainInfo> domainInfos = this.domainInfoService.selectDomainInfoList(domainInfo);
        mmap.put("domainInfoList", domainInfos);
        List<DomainObjectInfo> domainObjectInfos = new ArrayList<>();
        if (virtualUsageGroup.getDomainId() != null) {
            DomainInfo domainInfo1 = this.domainInfoService.selectDomainInfoById(virtualUsageGroup.getDomainId());
            if (domainInfo1 != null && domainInfo1.getDomainObjectInfoList() != null) {
                domainObjectInfos = domainInfo1.getDomainObjectInfoList();
            }
        } else {
            if (CollectionUtil.isNotEmpty(domainInfos)) {
                DomainInfo domainInfo1 = this.domainInfoService.selectDomainInfoById(domainInfos.get(0).getId());
                if (domainInfo1 != null && domainInfo1.getDomainObjectInfoList() != null) {
                    domainObjectInfos = domainInfo1.getDomainObjectInfoList();
                }
            }
        }
        mmap.put("domainObjectList", domainObjectInfos);
        return prefix + "/editVirtualUsageGroup";
    }

    /**
     * 修改保存虚拟用况组
     */
    //@RequiresPermissions("virtualUsageGroup:virtualUsageGroup:edit")
    @Log(title = "虚拟用况组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualUsageGroup virtualUsageGroup) {
        return toAjax(virtualUsageGroupService.updateVirtualUsageGroup(virtualUsageGroup));
    }

    /**
     * 删除虚拟用况组
     */
    //@RequiresPermissions("virtualUsageGroup:virtualUsageGroup:remove")
    @Log(title = "虚拟用况组", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualUsageGroupService.deleteVirtualUsageGroupByIds(ids));
    }

    @Autowired
    private IVirtualUsageService virtualUsageService;

    @Autowired
    private IVirtualUsageGroupDetailService virtualUsageGroupDetailService;

    @GetMapping("/virtual_usage_choose/{id}")
    public String virtual_usage_choose(@PathVariable("id") Long id, ModelMap mmap) {
        VirtualUsageGroup virtualUsageGroup = virtualUsageGroupService.selectVirtualUsageGroupById(id);
        mmap.put("virtualUsageGroup", virtualUsageGroup);

        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceId(virtualUsageGroup.getInterfaceId());
        List<VirtualUsage> virtualUsageList = this.virtualUsageService.selectVirtualUsages(virtualUsage);
        mmap.put("virtualUsageList", virtualUsageList);

        VirtualUsageGroupDetail virtualUsageGroupDetail = new VirtualUsageGroupDetail();
        virtualUsageGroupDetail.setGroupId(id);
        List<VirtualUsageGroupDetail> virtualUsageGroupDetails = virtualUsageGroupDetailService.selectVirtualUsageGroupDetailList(virtualUsageGroupDetail);
        mmap.put("virtualUsageGroupDetails", virtualUsageGroupDetails);
        return prefix + "/virtual_usage_choose";
    }

    @PostMapping("/virtualUsageList")
    @ResponseBody
    public AjaxResult virtualUsageList(Long id) {
        List<VirtualUsage> virtualUsageList = virtualUsageGroupDetailService.virtualUsageList(id);
        return AjaxResult.success(virtualUsageList);
    }

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    /**
     * 字段参数选择页面
     * @param interfaceId 接口id
     * @param isInParam 1 入参 2 出参
     * @param mmap mmap
     * @return
     */
    @GetMapping("field_choose/{interfaceId}/{isInParam}")
    public String field_choose(@PathVariable("interfaceId") Long interfaceId,
                                   @PathVariable("isInParam") String isInParam,
                                   ModelMap mmap) {
        mmap.put("interfaceId", interfaceId);
        mmap.put("isInParam", isInParam);
        List<VirtualUsage> virtualUsageList = virtualUsageGroupDetailService.virtualUsageList(interfaceId);
        ListInterfaceFieldParamReq listInterfaceFieldParamReq = new ListInterfaceFieldParamReq();
        listInterfaceFieldParamReq.setType("5");
        listInterfaceFieldParamReq.setInterfaceId(interfaceId);
        listInterfaceFieldParamReq.setIsInParam(isInParam);
        List<InterfaceParam> listFieldParam = this.interfaceInfoService.listInterfaceFieldParam(listInterfaceFieldParamReq);
        mmap.put("listFieldParam", listFieldParam);
        mmap.put("virtualUsageList", virtualUsageList);
        return prefix + "/field_choose";
    }

    @Autowired
    private IParamsFieldRelationService paramsFieldRelationService;

    @Autowired
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;

    /**
     * 保存bff接口参数
     * @param bffParamReq
     * @return
     */
    @PostMapping("/saveGroupParam")
    @ResponseBody
    public AjaxResult saveGroupParam(BffParamReq bffParamReq) {
        String[] fieldInfos = bffParamReq.getFieldInfo().split(",");
        List<Long> interfaceParamsFieldRelationIds = new ArrayList<>();
        List<Long> paramsFieldRelationIds = new ArrayList<>();
        for (String fieldInfo : fieldInfos) {
            if (StringUtils.isNotEmpty(fieldInfo)) {
                String[] split = fieldInfo.split("=");
                if ("1".equals(split[1])) {
                    interfaceParamsFieldRelationIds.add(Long.parseLong(split[0]));
                } else {
                    paramsFieldRelationIds.add(Long.parseLong(split[0]));
                }
            }
        }
        if (bffParamReq.getInterfaceParamsFieldRelationId() == null) {
            this.interfaceParamsFieldRelationService.saveGroupParam(bffParamReq, interfaceParamsFieldRelationIds, paramsFieldRelationIds);
        } else {
            this.paramsFieldRelationService.saveGroupParam(bffParamReq, interfaceParamsFieldRelationIds, paramsFieldRelationIds);
        }
        return AjaxResult.success();
    }

    /**
     * 数据集字段参数选择页面
     * @param interfaceId 接口id
     * @param isInParam 1 入参 2 出参
     * @param mmap mmap
     * @return
     */
    @GetMapping("params_field_choose/{interfaceId}/{isInParam}/{interfaceParamsFieldRelationId}")
    public String params_field_choose(@PathVariable("interfaceId") Long interfaceId,
                                          @PathVariable("isInParam") String isInParam,
                                          @PathVariable("interfaceParamsFieldRelationId") Long interfaceParamsFieldRelationId,
                                          ModelMap mmap) {
        mmap.put("interfaceId", interfaceId);
        mmap.put("isInParam", isInParam);
        mmap.put("interfaceParamsFieldRelationId", interfaceParamsFieldRelationId);

        List<VirtualUsage> virtualUsageList = virtualUsageGroupDetailService.virtualUsageList(interfaceId);
        ListInterfaceFieldParamReq listInterfaceFieldParamReq = new ListInterfaceFieldParamReq();
        listInterfaceFieldParamReq.setType("5");
        listInterfaceFieldParamReq.setInterfaceId(interfaceId);
        listInterfaceFieldParamReq.setIsInParam(isInParam);
        listInterfaceFieldParamReq.setInterfaceParamsFieldRelationId(interfaceParamsFieldRelationId);
        List<InterfaceParam> listFieldParam = this.interfaceInfoService.listInterfaceParamsField(listInterfaceFieldParamReq);
        mmap.put("listFieldParam", listFieldParam);
        mmap.put("virtualUsageList", virtualUsageList);
        return prefix + "/params_field_choose";
    }


    /**
     * 虚拟用况接口详情
     * @param id
     * @return
     */
    @GetMapping("/bff_usage_params")
    public String bff_usage_params(Long id, Long virtualUsageGroupId, ModelMap mmap) {
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setId(id);
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
        mmap.put("virtualUsage", virtualUsages.get(0));
        mmap.put("virtualUsageGroupId", virtualUsageGroupId);
        return prefix + "/bff_usage_params";
    }

    @Autowired
    private IVirtualUsageParamSourceService virtualUsageParamSourceService;

    /**
     * 字段来源选择
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceSelect")
    public String fieldSourceSelect(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
            // 查询字段对应的数据集
            ParamsFieldRelation paramsFieldRelation = this.paramsFieldRelationService.selectParamsFieldRelationById(virtualUsageParamSourcePo.getInterfaceFieldId());
            VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
            virtualUsageParamSource.setObjectId(paramsFieldRelation.getInterfaceDataListId());
            virtualUsageParamSource.setType("1");
            List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(virtualUsageParamSource);
            if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
                // 查询字段所对应数据集的来源数据集下字段集合
                Long sourceObjectId = virtualUsageParamSources.get(0).getSourceobjectId();
                // 查询来源数据集 接口名称
                String interfaceName = null;
                InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationById(sourceObjectId);
                if (interfaceParamsFieldRelation.getType().equals("3")) {
                    // 虚拟用况接口
                    VirtualUsage virtualUsage = new VirtualUsage();
                    virtualUsage.setId(interfaceParamsFieldRelation.getInterfaceId());
                    List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
                    interfaceName = virtualUsages.get(0).getSlaveInterfaceName();
                } else if(interfaceParamsFieldRelation.getType().equals("5")) {
                    VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(interfaceParamsFieldRelation.getInterfaceId());
                    interfaceName = virtualUsageGroup.getGroupName();
                } else {
                    InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceParamsFieldRelation.getInterfaceId());
                    interfaceName = interfaceInfo.getInterfaceName();
                }
                List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                        virtualUsageParamSourcePo.getFbType(),
                        null, virtualUsageParamSourcePo.getInterfaceFieldType(), sourceObjectId);
                interfaceObjectParamList.add(new InterfaceObjectParam(index, interfaceName, objectParamList));
            }
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
            VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(virtualUsageParamSourcePo.getInterfaceId());
            // 查询组接口入参
            List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                    "5",
                    "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
            interfaceObjectParamList.add(new InterfaceObjectParam(index ++, virtualUsageGroup.getGroupName(), objectParamList));
            // 查询虚拟用况
            List<VirtualUsage> virtualUsages = virtualUsageGroupDetailService.virtualUsageList(virtualUsageParamSourcePo.getInterfaceId());
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                for (VirtualUsage usage : virtualUsages) {
                    if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                        objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                                "3",
                                "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                        break;
                    }
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "3",
                            "2", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                }
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList2(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("id", "");
        }

        return prefix + "/field_source_choose";
    }
    /**
     * 数据集字段来源选择字段
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceSelect2")
    public String fieldSourceSelect2(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
        // 查询接口（bff/dubbo）入参
        String interfaceFieldType = "1";
        List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                "5",
                "1", interfaceFieldType, null);
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
        // 查询虚拟用况
        List<VirtualUsage> virtualUsages = virtualUsageGroupDetailService.virtualUsageList(virtualUsageParamSourcePo.getInterfaceId());
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            for (VirtualUsage usage : virtualUsages) {
                if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "3",
                            "1", interfaceFieldType, null);
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                    break;
                }
                objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                        "3",
                        "2", interfaceFieldType, null);
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        modelMap.put("type", "1");
        paramSource.setType("2");
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("id", "");
        }

        return prefix + "/field_source_choose2";
    }

    /**
     * 字段来源选择(字段、数据集、数据集字段全部展示)
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceAllSelect")
    public String fieldSourceAllSelect(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();

        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        paramSource.setVirtualUsageGroupId(virtualUsageParamSourcePo.getInterfaceId());
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
        }
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList2(paramSource);
        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("sourceType", virtualUsageParamSources.get(0).getSourceType());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("sourceType", "");
            modelMap.put("id", "");
        }
        int index = 1;
        VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(virtualUsageParamSourcePo.getInterfaceId());
        List<InterfaceParam> interfaceParams = this.interfaceInfoService.paramList("5", virtualUsageParamSourcePo.getInterfaceId(), "1");
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, virtualUsageGroup.getGroupName(), VirtualUsageParamSourceController.convertObjectParamList(interfaceParams)));
        // 查询虚拟用况
        List<VirtualUsage> virtualUsages = virtualUsageGroupDetailService.virtualUsageList(virtualUsageParamSourcePo.getInterfaceId());
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            for (VirtualUsage usage : virtualUsages) {
                if ("Y".equals(usage.getIsMaster())) {
                    usage.setSlaveInterfaceName(usage.getSlaveInterfaceName() + " （主）");
                }
                if (usage.getId().equals(virtualUsageParamSourcePo.getVirtualUsageId())) {
                    interfaceParams = this.interfaceInfoService.paramList("3", usage.getId(), "1");
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), VirtualUsageParamSourceController.convertObjectParamList(interfaceParams)));
                    break;
                }
                interfaceParams = this.interfaceInfoService.paramList("3", usage.getId(), "2");
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), VirtualUsageParamSourceController.convertObjectParamList(interfaceParams)));
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        return prefix + "/field_source_choose";
    }

    @GetMapping("/virtualUsageGroupModelSelect")
    public String virtualUsageGroupModelSelect(Long id, ModelMap modelMap) {
        VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(id);
        modelMap.put("virtualUsageGroup", virtualUsageGroup);
        return prefix + "/virtualUsageGroupModelSelect";
    }

    @PostMapping("/editModel")
    @ResponseBody
    public AjaxResult editModel(VirtualUsageGroup virtualUsageGroup) {
        this.virtualUsageGroupService.editModel(virtualUsageGroup);
        return success();
    }
}
